package com.jdy.haoduoaiteacher.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jdy.haoduoaiteacher.model.LeHotDynamicItem;
import com.jdy.haoduoaiteacher.util.HttpUtils;
import com.jdy.haoduoaiteacher.util.Tools;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.R;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/4/25 0025.
 */

public class MyStoryAdapter extends BaseAdapter {

    private final Context mContext;

    private final LayoutInflater mInflater;

    private final SparseArray<View> mSpArray;

    private ArrayList<LeHotDynamicItem> mMyStoryItemList;


    private OnMyStoryCheckedListener onMyStoryCheckedListener;

    private OnPreviewClickListener onPreviewClickListener;

    public void setOnMyStoryCheckedListener(OnMyStoryCheckedListener onMyStoryCheckedListener) {
        this.onMyStoryCheckedListener = onMyStoryCheckedListener;
    }

    public void setOnPreviewClickListener(OnPreviewClickListener onPreviewClickListener) {
        this.onPreviewClickListener = onPreviewClickListener;
    }

    public MyStoryAdapter(Context ctx, ArrayList<LeHotDynamicItem> list) {
        this.mContext = ctx;
        this.mMyStoryItemList = list;
        this.mInflater = LayoutInflater.from(mContext);
        this.mSpArray = new SparseArray<View>();
    }

    public void setData(ArrayList<LeHotDynamicItem> list) {
        mMyStoryItemList = list;
    }

    public void remove(LeHotDynamicItem object) {
        mMyStoryItemList.remove(object);
        notifyDataSetChanged();
    }

    public ArrayList<LeHotDynamicItem> getList() {
        return mMyStoryItemList;
    }

    /*
     * (non-Javadoc)
     *
     * @see android.widget.Adapter#getCount()
     */
    @Override
    public int getCount() {
        if (mMyStoryItemList != null)
            return mMyStoryItemList.size();
        else
            return 0;
    }

    /*
     * (non-Javadoc)
     *
     * @see android.widget.Adapter#getItem(int)
     */
    @Override
    public LeHotDynamicItem getItem(int position) {
        if (mMyStoryItemList != null)
            return mMyStoryItemList.get(position);
        else
            return null;
    }

    /*
     * (non-Javadoc)
     *
     * @see android.widget.Adapter#getItemId(int)
     */
    @Override
    public long getItemId(int position) {
        if (mMyStoryItemList != null)
            return position;
        else
            return 0;
    }

    public int selected_position = -1;

    /*
     * (non-Javadoc)
     *
     * @see android.widget.Adapter#getView(int, android.view.View,
     * android.view.ViewGroup)
     */
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final MyStoryAdapter.ViewHolder holder;
        // convertView = mSpArray.get(position);
        final LeHotDynamicItem my_story_item = mMyStoryItemList.get(position);
        if (convertView == null) {
            holder = new MyStoryAdapter.ViewHolder();
            convertView = mInflater.inflate(R.layout.my_story_for_habit_item, null);
//      holder.touchAreaLayout = (RelativeLayout) convertView
//          .findViewById(R.id.touch_area_layout);
            holder.img = (ImageView) convertView
                    .findViewById(R.id.img);
            holder.my_story_title = (TextView) convertView.findViewById(R.id.my_story_title);
            holder.my_story_preview = (TextView) convertView.findViewById(R.id.my_story_preview);
            holder.selectCb = (CheckBox) convertView.findViewById(R.id.select_cb);
            holder.rela_parent = (RelativeLayout) convertView
                    .findViewById(R.id.rela_parent);

            convertView.setTag(holder);
            // mSpArray.put(position, convertView);
        } else {
            holder = (MyStoryAdapter.ViewHolder) convertView.getTag();
        }

        //final MyStoryAdapter.ViewHolder holder2 = holder;

        //  holder.descriptionTv.setText(ppi.desc_short);
        holder.my_story_title.setText(my_story_item.dub_name);

        if (Tools.isNotEmpty(my_story_item.dub_cover)) {
            if (my_story_item.dub_cover.contains("api.lexiaobao.net")) {
                Picasso.with(mContext).load(my_story_item.dub_cover).into(holder.img);
            } else {
                Picasso.with(mContext).load(HttpUtils.HOST_RESOURCE + my_story_item.dub_cover).into(holder.img);
            }
        }

        holder.my_story_preview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onPreviewClickListener != null) {
                    onPreviewClickListener.onPreviewClick(holder.my_story_preview,my_story_item.id, my_story_item.av);
                }
            }
        });

//        holder.selectCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (onMyStoryCheckedListener != null) {
//                    onMyStoryCheckedListener.onMyStoryChecked(my_story_item.id, my_story_item.av);
//                }
//
//                if (isChecked) {
//                    selected_position = position;
//                } else {
//                    selected_position = -1;
//                }
////                notifyDataSetChanged();
//            }
//        });

        if(selected_position == position) {
            holder.selectCb.setChecked(true);
        }
        else {
            holder.selectCb.setChecked(false);
        }

        holder.selectCb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (((CheckBox) view).isChecked())
                {
                    selected_position= position;
                }
                else
                {
                    selected_position=-1;
                }

                notifyDataSetChanged();
            }
        });

        holder.selectCb.setChecked(position == selected_position);

        return convertView;
    }

    private static class ViewHolder {
        ImageView img;
        TextView my_story_title;
        TextView my_story_preview;
        CheckBox selectCb;
        private RelativeLayout rela_parent;
        View slayout;
    }

}

